<!DOCTYPE html>
<html >

<head>

  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>第 3 章 tidyverse 数据格式 | 使用 tidyverse 的方式分析仪器测量的数据</title>
  <meta name="description" content="第 3 章 tidyverse 数据格式 | 使用 tidyverse 的方式分析仪器测量的数据">
  <meta name="generator" content="bookdown  and GitBook 2.6.7">

  <meta property="og:title" content="第 3 章 tidyverse 数据格式 | 使用 tidyverse 的方式分析仪器测量的数据" />
  <meta property="og:type" content="book" />
  
  <meta property="og:image" content="images/coverpage.png" />
  <meta property="og:description" content="第 3 章 tidyverse 数据格式 | 使用 tidyverse 的方式分析仪器测量的数据" />
  <meta name="github-repo" content="zhujiedong/tidydevanalysis" />

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="第 3 章 tidyverse 数据格式 | 使用 tidyverse 的方式分析仪器测量的数据" />
  
  <meta name="twitter:description" content="第 3 章 tidyverse 数据格式 | 使用 tidyverse 的方式分析仪器测量的数据" />
  <meta name="twitter:image" content="images/coverpage.png" />

<meta name="author" content="祝介东    北京力高泰科技有限公司">


<meta name="date" content="2019-03-21">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  
  
<link rel="prev" href="prerequisites.html">
<link rel="next" href="rioexport.html">
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />









<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
  { content: attr(title);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<link rel="stylesheet" href="css\style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">R 软件与仪器数据分析</a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>欢迎</a></li>
<li class="chapter" data-level="" data-path="frontmatter.html"><a href="frontmatter.html"><i class="fa fa-check"></i>前言</a></li>
<li class="chapter" data-level="" data-path="author.html"><a href="author.html"><i class="fa fa-check"></i>关于作者</a></li>
<li class="chapter" data-level="" data-path="copyright.html"><a href="copyright.html"><i class="fa fa-check"></i>版权</a></li>
<li class="part"><span><b>I Part I</b></span></li>
<li class="chapter" data-level="1" data-path="origin-tidyverse.html"><a href="origin-tidyverse.html"><i class="fa fa-check"></i><b>1</b> tidyverse 的起源</a><ul>
<li class="chapter" data-level="1.1" data-path="origin-tidyverse.html"><a href="origin-tidyverse.html#tidyworkflow"><i class="fa fa-check"></i><b>1.1</b> tidyverse 的流程</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="prerequisites.html"><a href="prerequisites.html"><i class="fa fa-check"></i><b>2</b> 准备工作</a></li>
<li class="part"><span><b>II Part II</b></span></li>
<li class="chapter" data-level="3" data-path="readr.html"><a href="readr.html"><i class="fa fa-check"></i><b>3</b> tidyverse 数据格式</a><ul>
<li class="chapter" data-level="3.1" data-path="readr.html"><a href="readr.html#attridiff"><i class="fa fa-check"></i><b>3.1</b> 属性的差别</a></li>
<li class="chapter" data-level="3.2" data-path="readr.html"><a href="readr.html#parse-str"><i class="fa fa-check"></i><b>3.2</b> 数据类型的解析</a><ul>
<li class="chapter" data-level="3.2.1" data-path="readr.html"><a href="readr.html#factor-str"><i class="fa fa-check"></i><b>3.2.1</b> 因子类型</a></li>
<li class="chapter" data-level="3.2.2" data-path="readr.html"><a href="readr.html#datetime"><i class="fa fa-check"></i><b>3.2.2</b> 时间与日期</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="readr.html"><a href="readr.html#julian-day-"><i class="fa fa-check"></i><b>3.3</b> Julian day 的转换</a></li>
<li class="chapter" data-level="3.4" data-path="readr.html"><a href="readr.html#package-"><i class="fa fa-check"></i><b>3.4</b> 常用 package 介绍</a></li>
<li class="chapter" data-level="3.5" data-path="readr.html"><a href="readr.html#corereadr"><i class="fa fa-check"></i><b>3.5</b> readr 包核心函数</a></li>
<li class="chapter" data-level="3.6" data-path="readr.html"><a href="readr.html#corereadxl"><i class="fa fa-check"></i><b>3.6</b> readxl</a></li>
<li class="chapter" data-level="3.7" data-path="readr.html"><a href="readr.html#featherrds"><i class="fa fa-check"></i><b>3.7</b> 二进制文件</a></li>
<li class="chapter" data-level="3.8" data-path="readr.html"><a href="readr.html#rio"><i class="fa fa-check"></i><b>3.8</b> <code>rio</code>–万能的瑞士军刀</a><ul>
<li class="chapter" data-level="3.8.1" data-path="readr.html"><a href="readr.html#insrio"><i class="fa fa-check"></i><b>3.8.1</b> 安装</a></li>
<li class="chapter" data-level="3.8.2" data-path="readr.html"><a href="readr.html#imp-rio"><i class="fa fa-check"></i><b>3.8.2</b> 数据的读取</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="rioexport.html"><a href="rioexport.html"><i class="fa fa-check"></i><b>4</b> 使用 <code>rio</code> 导出数据</a><ul>
<li class="chapter" data-level="4.1" data-path="rioexport.html"><a href="rioexport.html#rioconvert"><i class="fa fa-check"></i><b>4.1</b> 数据的转换</a></li>
</ul></li>
<li class="part"><span><b>III Part III</b></span></li>
<li class="chapter" data-level="5" data-path="tidydata.html"><a href="tidydata.html"><i class="fa fa-check"></i><b>5</b> 数据的转换</a><ul>
<li class="chapter" data-level="5.1" data-path="tidydata.html"><a href="tidydata.html#tidyr"><i class="fa fa-check"></i><b>5.1</b> 使用 <code>tidyr</code> 清洁数据</a><ul>
<li class="chapter" data-level="5.1.1" data-path="tidydata.html"><a href="tidydata.html#gather"><i class="fa fa-check"></i><b>5.1.1</b> <code>gather</code> 用于合并数据集</a></li>
<li class="chapter" data-level="5.1.2" data-path="tidydata.html"><a href="tidydata.html#spread"><i class="fa fa-check"></i><b>5.1.2</b> <code>spread</code> 用于展开数据集</a></li>
<li class="chapter" data-level="5.1.3" data-path="tidydata.html"><a href="tidydata.html#separate"><i class="fa fa-check"></i><b>5.1.3</b> <code>separate</code> 用于单列数据的分离</a></li>
<li class="chapter" data-level="5.1.4" data-path="tidydata.html"><a href="tidydata.html#unite"><i class="fa fa-check"></i><b>5.1.4</b> <code>unite</code> 整合不同的列</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="6" data-path="dplyr.html"><a href="dplyr.html"><i class="fa fa-check"></i><b>6</b> <code>dplyr</code>–数据操作的语法</a></li>
<li class="chapter" data-level="7" data-path="todo.html"><a href="todo.html"><i class="fa fa-check"></i><b>7</b> todo</a><ul>
<li class="chapter" data-level="7.1" data-path="todo.html"><a href="todo.html#data-transformation"><i class="fa fa-check"></i><b>7.1</b> data transformation</a></li>
<li class="chapter" data-level="7.2" data-path="todo.html"><a href="todo.html#data-visulization"><i class="fa fa-check"></i><b>7.2</b> data visulization</a></li>
<li class="chapter" data-level="7.3" data-path="todo.html"><a href="todo.html#model-r"><i class="fa fa-check"></i><b>7.3</b> model r</a></li>
<li class="chapter" data-level="7.4" data-path="todo.html"><a href="todo.html#public"><i class="fa fa-check"></i><b>7.4</b> public</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>参考文献</a></li>
<li class="divider"></li>
<li><a href="https://bookdown.org" target="blank">由 bookdown 驱动</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">使用 tidyverse 的方式分析仪器测量的数据</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="readr" class="section level1">
<h1><span class="header-section-number">第 3 章</span> tidyverse 数据格式</h1>
<p>基本的数据导入，这里我们先通过 tidyverse 中的核心包为<code>readr</code>，引入 tidyverse 对数据导入的核心观点，如果熟悉 <code>read.csv</code> 或其他相似的函数，那么使用 <code>readr</code> 包必然也没有障碍，那么我们先用同样的两种方式，导入相同的 csv 文件，观察其差别：</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(tidyverse)</code></pre>
<pre><code>## -- Attaching packages ---------------- tidyverse 1.2.1.9000 --</code></pre>
<pre><code>## √ ggplot2 3.1.0     √ purrr   0.2.5
## √ tibble  2.0.1     √ dplyr   0.7.8
## √ tidyr   0.8.2     √ stringr 1.3.1
## √ readr   1.3.1     √ forcats 0.3.0</code></pre>
<pre><code>## -- Conflicts ------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()</code></pre>
<pre class="sourceCode r"><code class="sourceCode r">df &lt;-<span class="st"> </span><span class="kw">read_csv</span>(<span class="st">&quot;./data/aci_ex.csv&quot;</span>, <span class="dt">local =</span> <span class="kw">locale</span>(<span class="dt">encoding =</span> <span class="st">&quot;latin1&quot;</span>))</code></pre>
<pre><code>## Parsed with column specification:
## cols(
##   .default = col_double(),
##   date = col_datetime(format = &quot;&quot;),
##   LightAdaptedID = col_character(),
##   DarkPulseID = col_character()
## )</code></pre>
<pre><code>## See spec(...) for full column specifications.</code></pre>
<pre class="sourceCode r"><code class="sourceCode r">df</code></pre>
<pre><code>## # A tibble: 22 x 104
##      obs   time elapsed date                  TIME       E        A    Ci
##    &lt;dbl&gt;  &lt;dbl&gt;   &lt;dbl&gt; &lt;dttm&gt;               &lt;dbl&gt;   &lt;dbl&gt;    &lt;dbl&gt; &lt;dbl&gt;
##  1     1 1.48e9   7372. 2016-10-02 11:05:33 1.48e9 0.00770  29.5    275. 
##  2     2 1.48e9   7374. 2016-10-02 11:05:35 1.48e9 0.00781  26.3    239. 
##  3     3 1.48e9   7376. 2016-10-02 11:05:37 1.48e9 0.00790  22.8    205. 
##  4     4 1.48e9   7378. 2016-10-02 11:05:39 1.48e9 0.00799  18.6    173. 
##  5     5 1.48e9   7380. 2016-10-02 11:05:41 1.48e9 0.00806  14.4    140. 
##  6     6 1.48e9   7382. 2016-10-02 11:05:43 1.48e9 0.00822   9.73   110. 
##  7     7 1.48e9   7384. 2016-10-02 11:05:45 1.48e9 0.00835   5.01    78.8
##  8     8 1.48e9   7386. 2016-10-02 11:05:47 1.48e9 0.00839  -0.0205  48.5
##  9     9 1.48e9   7388. 2016-10-02 11:05:49 1.48e9 0.00860  28.8    286. 
## 10    10 1.48e9   7390. 2016-10-02 11:05:51 1.48e9 0.00875  34.9    362. 
## # ... with 12 more rows, and 96 more variables: Pci &lt;dbl&gt;, Pca &lt;dbl&gt;,
## #   gsw &lt;dbl&gt;, gbw &lt;dbl&gt;, gtw &lt;dbl&gt;, gtc &lt;dbl&gt;, Rabs &lt;dbl&gt;, TleafEB &lt;dbl&gt;,
## #   TleafCnd &lt;dbl&gt;, SVPleaf &lt;dbl&gt;, RHcham &lt;dbl&gt;, VPcham &lt;dbl&gt;,
## #   SVPcham &lt;dbl&gt;, VPDleaf &lt;dbl&gt;, blfa_1 &lt;dbl&gt;, blfa_2 &lt;dbl&gt;,
## #   blfa_3 &lt;dbl&gt;, DarkAdaptedID &lt;dbl&gt;, Fo &lt;dbl&gt;, Fm &lt;dbl&gt;, Fv &lt;dbl&gt;,
## #   Fv.Fm &lt;dbl&gt;, Adark &lt;dbl&gt;, LightAdaptedID &lt;chr&gt;, Fs &lt;dbl&gt;, Fm. &lt;dbl&gt;,
## #   PhiPS2 &lt;dbl&gt;, PS2.1 &lt;dbl&gt;, Qabs_fs &lt;dbl&gt;, Afs &lt;dbl&gt;, ETR &lt;dbl&gt;,
## #   Fv..Fm. &lt;dbl&gt;, PhiCO2 &lt;dbl&gt;, NPQ &lt;dbl&gt;, DarkPulseID &lt;chr&gt;, Fo. &lt;dbl&gt;,
## #   Fv. &lt;dbl&gt;, qP &lt;dbl&gt;, qN &lt;dbl&gt;, qP_Fo &lt;dbl&gt;, qN_Fo &lt;dbl&gt;, Qin &lt;dbl&gt;,
## #   Qabs &lt;dbl&gt;, alpha &lt;dbl&gt;, convert &lt;dbl&gt;, TIME.1 &lt;dbl&gt;, CO2_s &lt;dbl&gt;,
## #   CO2_r &lt;dbl&gt;, H2O_s &lt;dbl&gt;, H2O_r &lt;dbl&gt;, Flow &lt;dbl&gt;, Pa &lt;dbl&gt;,
## #   `|¤Pcham` &lt;dbl&gt;, Tair &lt;dbl&gt;, Tleaf &lt;dbl&gt;, Tleaf2 &lt;dbl&gt;,
## #   Fan_speed &lt;dbl&gt;, Qamb_in &lt;dbl&gt;, Qamb_out &lt;dbl&gt;, Q &lt;dbl&gt;, f_red &lt;dbl&gt;,
## #   f_blue &lt;dbl&gt;, f_farred &lt;dbl&gt;, F &lt;dbl&gt;, Q_modavg &lt;dbl&gt;, F_dc &lt;dbl&gt;,
## #   Tled &lt;dbl&gt;, TDigital &lt;dbl&gt;, TPreamp &lt;dbl&gt;, TPwrSpy &lt;dbl&gt;,
## #   TDrive &lt;dbl&gt;, F_avg &lt;dbl&gt;, dF.dt &lt;dbl&gt;, dF_dc.dt &lt;dbl&gt;,
## #   F_dc_avg &lt;dbl&gt;, period &lt;dbl&gt;, DIAG &lt;dbl&gt;, Flow_s &lt;dbl&gt;, Flow_r &lt;dbl&gt;,
## #   Txchg &lt;dbl&gt;, Tirga &lt;dbl&gt;, Tchopper &lt;dbl&gt;, Ts &lt;dbl&gt;, Tr &lt;dbl&gt;,
## #   CO2_. &lt;dbl&gt;, Desiccant_. &lt;dbl&gt;, Humidifier_. &lt;dbl&gt;,
## #   Heatx_setpoint &lt;dbl&gt;, CO2_r_setpoint &lt;dbl&gt;, H2O_r_setpoint &lt;dbl&gt;,
## #   SS_s &lt;dbl&gt;, SS_r &lt;dbl&gt;, MatchH2O &lt;dbl&gt;, MatchCO2 &lt;dbl&gt;,
## #   MatchValveR &lt;dbl&gt;, MatchValveS &lt;dbl&gt;</code></pre>
<pre class="sourceCode r"><code class="sourceCode r">df &lt;-<span class="st"> </span><span class="kw">read.csv</span>(<span class="st">&quot;./data/aci_ex.csv&quot;</span>)
<span class="kw">head</span>(df)</code></pre>
<pre><code>##   obs       time elapsed              date       TIME           E        A
## 1   1 1475427934  7372.4 20161002 11:05:33 1475427933 0.007696760 29.47670
## 2   2 1475427936  7374.4 20161002 11:05:35 1475427935 0.007808186 26.31897
## 3   3 1475427938  7376.4 20161002 11:05:37 1475427937 0.007901756 22.76927
## 4   4 1475427940  7378.4 20161002 11:05:39 1475427939 0.007987330 18.61361
## 5   5 1475427942  7380.4 20161002 11:05:41 1475427941 0.008064806 14.43679
## 6   6 1475427944  7382.4 20161002 11:05:43 1475427943 0.008215388  9.73141
##         Ci      Pci      Pca       gsw      gbw       gtw       gtc
## 1 274.6465 30.69809 38.03046 0.3432676 2.001378 0.4403924 0.2800793
## 2 239.3628 30.50294 37.78892 0.3481919 2.007294 0.4405757 0.2801836
## 3 205.2410 30.36986 37.54531 0.3530725 2.002956 0.4405515 0.2801784
## 4 172.8188 30.21500 37.29877 0.3561105 2.000471 0.4410542 0.2805097
## 5 140.4173 30.02464 37.05066 0.3602038 2.003897 0.4407989 0.2803362
## 6 109.8534 29.89953 36.80124 0.3671059 2.000833 0.4404739 0.2801333
##       Rabs  TleafEB TleafCnd  SVPleaf   RHcham   VPcham  SVPcham  VPDleaf
## 1 165.2454 25.96638  24.9904 3.177858 55.40208 1.979484 3.572942 1.198374
## 2 165.1947 25.96739  24.9941 3.178559 55.40164 1.979585 3.573152 1.198975
## 3 165.2454 25.97100  24.9860 3.177025 55.39090 1.979457 3.573614 1.197568
## 4 165.2454 25.96911  24.9840 3.176646 55.38870 1.979506 3.573845 1.197140
## 5 165.2437 25.97255  24.9865 3.177119 55.37738 1.979323 3.574244 1.197797
## 6 165.2454 25.97429  24.9857 3.176968 55.36981 1.979192 3.574496 1.197776
##        blfa_1     blfa_2   blfa_3 DarkAdaptedID Fo Fm Fv Fv.Fm Adark
## 1 -0.03479512 0.03906057 3.019639             0  0  0  0     0    -1
## 2 -0.03494065 0.03922395 3.029814             0  0  0  0     0    -1
## 3 -0.03483390 0.03910411 3.022352             0  0  0  0     0    -1
## 4 -0.03477282 0.03903553 3.018079             0  0  0  0     0    -1
## 5 -0.03485704 0.03913008 3.023969             0  0  0  0     0    -1
## 6 -0.03478173 0.03904554 3.018702             0  0  0  0     0    -1
##   LightAdaptedID Fs Fm. PhiPS2 PS2.1 Qabs_fs Afs ETR Fv..Fm.     PhiCO2
## 1              -  0   0      0   0.5       0   0   0       0 0.02699408
## 2              -  0   0      0   0.5       0   0   0       0 0.02683882
## 3              -  0   0      0   0.5       0   0   0       0 0.02643125
## 4              -  0   0      0   0.5       0   0   0       0 0.02611003
## 5              -  0   0      0   0.5       0   0   0       0 0.02588544
## 6              -  0   0      0   0.5       0   0   0       0 0.02539339
##   NPQ DarkPulseID Fo. Fv. qP qN qP_Fo qN_Fo      Qin     Qabs     alpha
## 1   0           -   0   0  0  0     0     0 1000.060 843.0848 0.8430005
## 2   0           -   0   0  0  0     0     0  999.706 843.0932 0.8430005
## 3   0           -   0   0  0  0     0     0 1000.040 842.8319 0.8430005
## 4   0           -   0   0  0  0     0     0  999.990 843.0924 0.8429996
## 5   0           -   0   0  0  0     0     0  999.973 843.0839 0.8429996
## 6   0           -   0   0  0  0     0     0  999.963 843.0755 0.8429996
##     convert     TIME.1   CO2_s   CO2_r   H2O_s   H2O_r    Flow      Pa
## 1 0.1960010 1475427933 367.719 400.026 23.5181 17.1985 599.683 83.9679
## 2 0.1960010 1475427935 321.236 350.063 23.5197 17.1997 600.208 83.9672
## 3 0.1960010 1475427937 275.105 300.048 23.5183 17.2012 599.744 83.9667
## 4 0.1959991 1475427939 229.601 250.048 23.5189 17.2009 600.127 83.9665
## 5 0.1959991 1475427941 184.130 200.052 23.5173 17.1997 600.164 83.9654
## 6 0.1959991 1475427943 139.148 150.023 23.5148 17.1972 599.687 83.9670
##     ΔPcham    Tair   Tleaf Tleaf2 Fan_speed Qamb_in Qamb_out       Q
## 1 0.200645 26.9704 25.0272  999.9   9993.12       0  6.93277 1000.10
## 2 0.199888 26.9714 24.9913  999.9  10035.00       0  6.93277 1000.11
## 3 0.199957 26.9736 24.9877  999.9  10004.40       0  6.93277 1000.11
## 4 0.200108 26.9747 25.0090  999.9   9986.88       0  6.93277  999.79
## 5 0.199135 26.9766 25.0247  999.9  10011.20       0  6.93277 1000.10
## 6 0.200911 26.9778 25.0009  999.9   9989.38       0  6.93277 1000.09
##      f_red    f_blue f_farred      F Q_modavg    F_dc   Tled TDigital
## 1 0.900015 0.0999851        0 2.8427        0 7893.66 38.812   41.812
## 2 0.899984 0.1000160        0 2.5274        0 7887.49 38.812   41.812
## 3 0.899984 0.1000160        0 2.6852        0 7878.68 38.812   41.812
## 4 0.899984 0.1000160        0 2.6955        0 7869.08 38.812   41.812
## 5 0.900015 0.0999851        0 2.5337        0 7865.47 38.812   41.812
## 6 0.900015 0.0999851        0 2.3220        0 7856.35 38.812   41.812
##   TPreamp TPwrSpy TDrive    F_avg      dF.dt  dF_dc.dt F_dc_avg period
## 1  40.437  40.625 39.187 2.617227 -0.5654120 -144.1986 7913.660     15
## 2  40.437  40.625 39.187 2.616973 -0.6446940 -161.5990 7909.170     15
## 3  40.437  40.625 39.187 2.585404 -0.5877231 -178.2270 7902.338     15
## 4  40.437  40.625 39.187 2.602727 -0.4243932 -191.5279 7896.840     15
## 5  40.437  40.625 39.187 2.579562 -0.1809162 -199.7292 7891.172     15
## 6  40.437  40.625 39.187 2.550577  0.1461197 -211.9402 7883.021     15
##   DIAG  Flow_s  Flow_r   Txchg   Tirga Tchopper      Ts      Tr   CO2_.
## 1    2 506.541 663.349 25.4141 27.5924  30.0001 27.5408 27.5134 29.9284
## 2    2 501.762 662.342 25.4212 27.5924  30.0001 27.5408 27.5134 29.7805
## 3    2 514.578 663.419 25.4292 27.5924  30.0000 27.5408 27.5134 29.5901
## 4    2 498.758 662.436 25.4351 27.5924  30.0000 27.5408 27.5134 29.3941
## 5    2 522.239 662.835 25.4384 27.5924  30.0000 27.5408 27.5134 29.2541
## 6    2 504.517 662.881 25.4413 27.5924  30.0000 27.5408 27.5134 29.0845
##   Desiccant_. Humidifier_. Heatx_setpoint CO2_r_setpoint H2O_r_setpoint
## 1      43.693            0        25.4324        460.667        17.2388
## 2      43.693            0        25.4324        460.667        17.2388
## 3      43.693            0        25.4390        455.833        17.2388
## 4      43.693            0        25.4390        450.667        17.2388
## 5      43.693            0        25.4390        450.667        17.2388
## 6      43.693            0        25.4491        445.833        17.2388
##      SS_s    SS_r MatchH2O MatchCO2 MatchValveR MatchValveS
## 1 100.741 101.259   -0.031    5.465         100         100
## 2 100.741 101.260   -0.031    5.465         100         100
## 3 100.742 101.261   -0.031    5.465         100         100
## 4 100.742 101.261   -0.031    5.465         100         100
## 5 100.742 101.261   -0.031    5.465         100         100
## 6 100.743 101.261   -0.031    5.465         100         100</code></pre>
<p>这里先不谈其他，比较一下代码的差别, <code>read_csv</code> 读取同样的我这一个文件，多了一个 <code>local = locale(encoding = "latin1")</code> 的设定，看上去复杂了，实际上多数情况是无需的，但我们这个文件内有希腊字母等存在，如果不实用就会提示 <code>input string 1 is invalid in this locale</code> 相关报错，在此不过多解释，相信大家看了下面关于 locale 的一个简单解释就明白了：</p>
<p><a href="https://docs.oracle.com/cd/E23824_01/html/E26033/glmbx.html">locale</a></p>
<div id="attridiff" class="section level2">
<h2><span class="header-section-number">3.1</span> 属性的差别</h2>
<p>我们看一下读取的数据的类有无差别：</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(tidyverse)
df1 &lt;-<span class="st"> </span><span class="kw">read_csv</span>(<span class="st">&quot;./data/aci_ex.csv&quot;</span>, <span class="dt">local =</span> <span class="kw">locale</span>(<span class="dt">encoding =</span> <span class="st">&quot;latin1&quot;</span>))</code></pre>
<pre><code>## Parsed with column specification:
## cols(
##   .default = col_double(),
##   date = col_datetime(format = &quot;&quot;),
##   LightAdaptedID = col_character(),
##   DarkPulseID = col_character()
## )</code></pre>
<pre><code>## See spec(...) for full column specifications.</code></pre>
<pre class="sourceCode r"><code class="sourceCode r">df2 &lt;-<span class="st"> </span><span class="kw">read.csv</span>(<span class="st">&quot;./data/aci_ex.csv&quot;</span>)
<span class="kw">class</span>(df1)</code></pre>
<pre><code>## [1] &quot;spec_tbl_df&quot; &quot;tbl_df&quot;      &quot;tbl&quot;         &quot;data.frame&quot;</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">class</span>(df2)</code></pre>
<pre><code>## [1] &quot;data.frame&quot;</code></pre>
<p>除了 data.frame 的属性外，<code>readr</code> 除了常规的 data.frame 的类外，还多了几个，它属于 tibble 的属性，我们不多说，看一下 Hadley 大神对 tibble 的解释：</p>

<div class="definition">
<span id="def:unnamed-chunk-3" class="definition"><strong>Definition 3.1  (tibble definition)  </strong></span>Tibbles are a modern reimagining of the data.frame, keeping what time has proven to be effective, and throwing out what is not.
</div>

<p>tibble 不同于 dataframe 的地方主要在打印和构造子数据集时：</p>
<ul>
<li><p>Tibbles 只显示前 10 行数据，但会显示所有列，因此大数据时比较方便，此外，除了显示列名外，他还回显示数据类型。</p></li>
<li><p>此外，使用 “$” 构造子集时 tibble 要求严格的变量名称。例如：</p></li>
</ul>
<pre class="sourceCode r"><code class="sourceCode r"><span class="co">#dataframe</span>
df &lt;-<span class="st"> </span><span class="kw">head</span>(iris)
<span class="co">#tibble</span>
tf &lt;-<span class="st"> </span><span class="kw">as_tibble</span>(iris)

<span class="co"># return results even with wrong name</span>
df<span class="op">$</span>Sepal.Leng</code></pre>
<pre><code>## [1] 5.1 4.9 4.7 4.6 5.0 5.4</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="co"># error</span>
tf<span class="op">$</span>Sepal.Leng</code></pre>
<pre><code>## Warning: Unknown or uninitialised column: &#39;Sepal.Leng&#39;.</code></pre>
<pre><code>## NULL</code></pre>
<ul>
<li>Tibbles 仍然可以使用 [ 和 [[: [ 返回的是另一个 tibble, 而 [[ 返回的是一个向量，不在需要 <code>drop = FALSE</code>!</li>
</ul>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">class</span>(iris[ , <span class="dv">1</span>, <span class="dt">drop =</span> <span class="ot">FALSE</span>])</code></pre>
<pre><code>## [1] &quot;data.frame&quot;</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">class</span>(<span class="kw">as_data_frame</span>(iris)[ , <span class="dv">1</span>])</code></pre>
<pre><code>## Warning: `as_data_frame()` is deprecated, use `as_tibble()` (but mind the new semantics).
## This warning is displayed once per session.</code></pre>
<pre><code>## [1] &quot;tbl_df&quot;     &quot;tbl&quot;        &quot;data.frame&quot;</code></pre>
<p><strong>注意：tibble 和 dataframe 还有一个重要区别是 tibble 不会将字符转换为因子，相当于使用 <code>stringsAsFactors = FALSE</code></strong></p>
</div>
<div id="parse-str" class="section level2">
<h2><span class="header-section-number">3.2</span> 数据类型的解析</h2>
<p>前面已经提到了，tibble 会自动解析数据的类型，但是这个类型是怎么解析的，解析错误怎么办，这就不得不提 <code>parse_*</code> 一系列函数了：</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">str</span>(<span class="kw">parse_logical</span>(<span class="kw">c</span>(<span class="st">&quot;TRUE&quot;</span>, <span class="st">&quot;FALSE&quot;</span>, <span class="st">&quot;NA&quot;</span>)))</code></pre>
<pre><code>##  logi [1:3] TRUE FALSE NA</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">str</span>(<span class="kw">parse_integer</span>(<span class="kw">c</span>(<span class="st">&quot;1&quot;</span>, <span class="st">&quot;2&quot;</span>, <span class="st">&quot;3&quot;</span>)))</code></pre>
<pre><code>##  int [1:3] 1 2 3</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">str</span>(<span class="kw">parse_date</span>(<span class="kw">c</span>(<span class="st">&quot;2011-01-01&quot;</span>, <span class="st">&quot;1997-10-14&quot;</span>)))</code></pre>
<pre><code>##  Date[1:2], format: &quot;2011-01-01&quot; &quot;1997-10-14&quot;</code></pre>
<p>需要注意的是，如果解析失败，在输出时会是 NA，这就是为什么一些他别乱的数据会出现大量 NA 的原因：</p>
<pre class="sourceCode r"><code class="sourceCode r">x &lt;-<span class="st"> </span><span class="kw">parse_integer</span>(<span class="kw">c</span>(<span class="st">&quot;123&quot;</span>, <span class="st">&quot;456&quot;</span>, <span class="st">&quot;abc&quot;</span>, <span class="st">&quot;123.321&quot;</span>))</code></pre>
<pre><code>## Warning: 2 parsing failures.
## row col               expected actual
##   3  -- an integer               abc 
##   4  -- no trailing characters   .321</code></pre>
<pre class="sourceCode r"><code class="sourceCode r">x</code></pre>
<pre><code>## [1] 123 456  NA  NA
## attr(,&quot;problems&quot;)
## # A tibble: 2 x 4
##     row   col expected               actual
##   &lt;int&gt; &lt;int&gt; &lt;chr&gt;                  &lt;chr&gt; 
## 1     3    NA an integer             abc   
## 2     4    NA no trailing characters .321</code></pre>
<p><code>parse_*</code> 有八个函数，他们遵循相同的语法结构，其中：</p>
<ul>
<li><p><code>parse_logical</code> 与 <code>parse_integer</code> 类型非常简单，不会出错（数据里面掺杂了其他类型那属于人的错误或机器的错误）。</p></li>
<li><p><code>parse_double</code> 则是严格的解析方式，必须是浮点类型的<a href="#fn2" class="footnote-ref" id="fnref2"><sup>2</sup></a>，而 <code>parse_number</code> 则相对宽松，我们的问题主要出现在万一你分析的是德国等欧洲国家的数据，反正我第一次看到德国人的发票愣是看了半天才想明白价格。</p></li>
<li><p><code>parse_character</code> 则是非常容易解析的类型，其复杂也在编码上，对于我们来讲，多数情况是不大可能出错的。</p></li>
<li><p><code>parse_factor</code> 功能是创建分类变量，对于我们实验数据时用的非常多的。</p></li>
<li><p><code>parse_datetime</code>, <code>parse_date</code>, <code>parse_time</code> 则是专门解析时间日期的，也是最复杂的，例如常见的英美仪器时间，ISO 时间以及 CR1000 所采用的 julia day 和 LI-6800 所采用的 unix time 或者叫做 POSIX time 或 Epoch Time。</p></li>
</ul>
<p>鉴于实际难度和我少打字的原则<a href="#fn3" class="footnote-ref" id="fnref3"><sup>3</sup></a>，后面我只简单介绍因子类和时间日期类。</p>
<div id="factor-str" class="section level3">
<h3><span class="header-section-number">3.2.1</span> 因子类型</h3>
<p>因子是 R 中用来创建分类数据的，例如 <code>fitacis</code> 用来批量处理不同处理或小区的光合数据，group 参数用的就是 factor 类型，当然，dataframe 格式自动帮我们完成了从 character 到 factor 的转换。当然，需要注意，factor 是有不同水平的，如同我们实验有处理，处理也要分不同的水平，我觉得这个角度理解 factor 非常实在，尤其是对于 农学、林学、生态背景的我们。如果数据里的水平是你后加的，加的时候漏掉了一个水平，那后果也不是很严重，就是分析不能继续，直到你找到错误：</p>
<pre class="sourceCode r"><code class="sourceCode r">fertilizer &lt;-<span class="st"> </span><span class="kw">c</span>(<span class="st">&quot;N&quot;</span>, <span class="st">&quot;P&quot;</span>, <span class="st">&quot;K&quot;</span>,<span class="st">&quot;CK&quot;</span>)
<span class="kw">parse_factor</span>(<span class="kw">c</span>(<span class="st">&quot;N&quot;</span>, <span class="st">&quot;P&quot;</span>, <span class="st">&quot;K&quot;</span>, <span class="st">&quot;CK&quot;</span>, <span class="st">&quot;NPK&quot;</span>), <span class="dt">levels =</span> fertilizer)</code></pre>
<pre><code>## Warning: 1 parsing failure.
## row col           expected actual
##   5  -- value in level set    NPK</code></pre>
<pre><code>## [1] N    P    K    CK   &lt;NA&gt;
## attr(,&quot;problems&quot;)
## # A tibble: 1 x 4
##     row   col expected           actual
##   &lt;int&gt; &lt;int&gt; &lt;chr&gt;              &lt;chr&gt; 
## 1     5    NA value in level set NPK   
## Levels: N P K CK</code></pre>
<p>如果不幸漏掉了 “NPK” 这个处理，那么这个解析就会报错，无法完成，直到你在 fertilizer 内增加了复合肥<a href="#fn4" class="footnote-ref" id="fnref4"><sup>4</sup></a>这个处理。</p>
<p><strong>如果更不幸的事情发生了，你排查不到错误，无法将字符转换为因子类型，那么无需着急，我们后面还有其他工具，此时就让他们作为字符类型好了</strong>。</p>
</div>
<div id="datetime" class="section level3">
<h3><span class="header-section-number">3.2.2</span> 时间与日期</h3>
<p><code>parse_datetime</code> 默认解析的方式非常符合中国人的习惯，采用的是 <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO8601</a>, 年月日及时间的方式，如同我最开始数据导入章节 <a href="readr.html#readr">3</a> 举例时，<code>read_csv</code> 十分准确的识别了这个时间。</p>
<p>对于仪器中长用的时间类型，无非是 ISO08601 的标准日期，julian day 日期格式<a href="#fn5" class="footnote-ref" id="fnref5"><sup>5</sup></a>，POSIX 时间<a href="#fn6" class="footnote-ref" id="fnref6"><sup>6</sup></a>，以及欧美时间的格式，处理起来各不相同。这里对时间日期的处理采用 <code>lubridate</code> 来处理：</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(lubridate)</code></pre>
<pre><code>## 
## Attaching package: &#39;lubridate&#39;</code></pre>
<pre><code>## The following object is masked from &#39;package:base&#39;:
## 
##     date</code></pre>
<div id="iso08601-" class="section level4">
<h4><span class="header-section-number">3.2.2.1</span> ISO08601 时间及欧美时间的处理</h4>
<p>这种时间日期的导入绝大部分能够正常识别，如果不能可以以字符型导入，然后进行解析:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ymd_hms</span>(<span class="st">&quot;2019-02-25 21:28:59&quot;</span>)</code></pre>
<pre><code>## [1] &quot;2019-02-25 21:28:59 UTC&quot;</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">mdy_hms</span>(<span class="st">&quot;02-25-2019 21:28:59&quot;</span>)</code></pre>
<pre><code>## [1] &quot;2019-02-25 21:28:59 UTC&quot;</code></pre>
</div>
</div>
</div>
<div id="julian-day-" class="section level2">
<h2><span class="header-section-number">3.3</span> Julian day 的转换</h2>
<p>这个日期尽管看起来奇怪，但是要转换比较容易，<code>base</code> 包即可以。不过我们需要指定起始日期，不然仪器不知道怎么去做，当然，我们既然使用了 <code>lubridate</code>，它也有对应的函数：</p>
<pre class="sourceCode r"><code class="sourceCode r">jday &lt;-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">8</span>, <span class="dv">20</span>, <span class="dv">370</span>)
<span class="kw">as_date</span>(jday, <span class="dt">origin=</span><span class="st">&quot;2018-01-01&quot;</span>)</code></pre>
<pre><code>## [1] &quot;2018-01-02&quot; &quot;2018-01-09&quot; &quot;2018-01-21&quot; &quot;2019-01-06&quot;</code></pre>
<p>这样就一下子搞定了我们的茎流数据的日期了。</p>
<div id="posix-" class="section level4">
<h4><span class="header-section-number">3.3.0.1</span> POSIX 日期的转换</h4>
<p>这个对于广大程序员来讲非常熟悉，但是对于我们生态环境行业来讲，是对人类很不友好的数据格式，但对于转换来讲,其实也非常简单,如果是有具体的时间，则表示以秒为单位计算，我们可以使用 <code>as_datetime</code>：</p>
<pre class="sourceCode r"><code class="sourceCode r">epoch &lt;-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">100</span>, <span class="fl">2.1e+8</span>)
<span class="kw">as_datetime</span>(epoch)</code></pre>
<pre><code>## [1] &quot;1970-01-01 00:00:01 UTC&quot; &quot;1970-01-01 00:01:40 UTC&quot;
## [3] &quot;1976-08-27 13:20:00 UTC&quot;</code></pre>


</div>
</div>
<div id="package-" class="section level2">
<h2><span class="header-section-number">3.4</span> 常用 package 介绍</h2>
</div>
<div id="corereadr" class="section level2">
<h2><span class="header-section-number">3.5</span> readr 包核心函数</h2>
<p>我们常用的函数，借用 <a href="https://www.rstudio.com/resources/cheatsheets/">readr cheetsheet</a> 来展示一下其主要用途：</p>
<div class="figure"><span id="fig:readrpng"></span>
<img src="http://wx2.sinaimg.cn/large/8efe7bc9ly1g0n7a72q7mj20h90a1dhe.jpg" alt="readr 常用函数图解"  />
<p class="caption">
图 3.1: readr 常用函数图解
</p>
</div>
</div>
<div id="corereadxl" class="section level2">
<h2><span class="header-section-number">3.6</span> readxl</h2>
<p>对于读取 excel 格式的文件，一个函数就足够了：</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">read_excel</span>(path, <span class="dt">sheet =</span> <span class="ot">NULL</span>, <span class="dt">range =</span> <span class="ot">NULL</span>, <span class="dt">col_names =</span> <span class="ot">TRUE</span>,
  <span class="dt">col_types =</span> <span class="ot">NULL</span>, <span class="dt">na =</span> <span class="st">&quot;&quot;</span>, <span class="dt">trim_ws =</span> <span class="ot">TRUE</span>, <span class="dt">skip =</span> <span class="dv">0</span>,
  <span class="dt">n_max =</span> <span class="ot">Inf</span>, <span class="dt">guess_max =</span> <span class="kw">min</span>(<span class="dv">1000</span>, n_max),
  <span class="dt">progress =</span> <span class="kw">readxl_progress</span>(), <span class="dt">.name_repair =</span> <span class="st">&quot;unique&quot;</span>)</code></pre>
<p>自动判断 xls 或者 xlsx 格式来读取，遗憾的是，对于 LI-6400 的 xls 格式，它无能为力，无法读取，对于 LI-6800 的 xlsx 格式，不能正确的识别其公式，因为表头太多了，如果单纯只有测量数据的文件，它是没有问题的。</p>
</div>
<div id="featherrds" class="section level2">
<h2><span class="header-section-number">3.7</span> 二进制文件</h2>
<p>此处的二进制文件特指 R 原生的 rds 格式和最新的 feather 格式。他们都是轻量级的数据格式，feather 的优势是能够保存 tibble 数据解析的数据格式。至于读取相应类型的数据，则非常容易，对于我们来讲，其读取的意义在于我们将大量的其他格式数据存储为这两种格式后，进行读取，直接是这两种格式的仪器数据，目前来讲应该还没有。</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">readRDS</span>(file, <span class="dt">refhook =</span> <span class="ot">NULL</span>)</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">read_feather</span>(path, <span class="dt">columns =</span> <span class="ot">NULL</span>)</code></pre>
</div>
<div id="rio" class="section level2">
<h2><span class="header-section-number">3.8</span> <code>rio</code>–万能的瑞士军刀</h2>
<p>前面几节介绍读取数据的函数是走马观花，蜻蜓点水的方式，不是故意偷懒，是因为本节内容才是重点，之所以标题将 <code>rio</code> 称之为万能的瑞士军刀，是因为其功能决定的： <code>rio</code> 实际上属于作者对 <code>data.table</code>、<code>haven</code>、<code>readxl</code> 等一系列软件包相关函数的打包，然后将格式的识别自动化，减少了我们的工作量，主要特点为：</p>
<ul>
<li><p>使用扩展名来识别文件类型，减少人工输入的工作量，若是格式无法识别，则可以通过指定 format 来导入。</p></li>
<li><p><a href="https://cran.r-project.org/web/packages/reader/index.html">reader</a> 用来处理常见的文本数据，无需指定特定的数据类型。</p></li>
<li><p><a href="https://cran.r-project.org/package=io">io</a> 处理自定义数据格式。</p></li>
<li><p><a href="https://cran.r-project.org/package=ImportExport">ImportExport</a> 集中于处理 excel、SPSS 等二进制文件并提供 shiny 的界面。</p></li>
<li><p><a href="https://cran.r-project.org/package=SchemaOnRead">SchemaOnRead</a> 则是通过一系列的迭代找到最合适的读取数据的方法。</p></li>
</ul>
<p>值得一提的是，<code>rio</code> 尽管使用了 base 函数读取数据，但他从不将字符串当作因子类型处理，遵循 tidyverse 的原则。我们通过举例来完成相关函数的介绍：</p>
<div id="insrio" class="section level3">
<h3><span class="header-section-number">3.8.1</span> 安装</h3>
<p><code>rio</code> 安装略微不同于其他软件包，安装好之后，我们最好通过 <code>install_formats()</code> 命令进一步安装其他缺失的软件包，以进一步获得完整的数据格式的支持。</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">install.packages</span>(<span class="st">&quot;rio&quot;</span>)
<span class="kw">library</span>(rio)
<span class="kw">install_formats</span>()</code></pre>
</div>
<div id="imp-rio" class="section level3">
<h3><span class="header-section-number">3.8.2</span> 数据的读取</h3>
<p>读取数据时，我们可以忘掉原来那一系列函数，只需记得 <code>import</code> 即可：</p>
<div id="riocsv" class="section level4">
<h4><span class="header-section-number">3.8.2.1</span> 读取 csv 格式数据</h4>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(rio)
aci &lt;-<span class="st"> </span><span class="kw">import</span>(<span class="st">&quot;./data/aci6800.csv&quot;</span>)</code></pre>
<p>当然，这不足以显示 rio 的优势，因为这种简单格式对 <code>read.csv</code> 也不费力，那我们来点高级的，我这里有一个文件夹，里面放了 4 个 csv 文件：</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(plantecophys)
paths &lt;-<span class="st"> </span><span class="kw">Sys.glob</span>(<span class="st">&quot;./data/multi_csv/*.csv&quot;</span>)
all_data &lt;-<span class="st"> </span><span class="kw">import_list</span>(paths)
fits &lt;-<span class="st"> </span><span class="kw">lapply</span>(all_data, fitaci, <span class="dt">fitmethod =</span> <span class="st">&quot;bilinear&quot;</span>)
fits<span class="op">$</span>aci4<span class="op">$</span>pars</code></pre>
<pre><code>##          Estimate Std. Error
## Vcmax  49.3787547  3.4815555
## Jmax  128.5546403         NA
## Rd      0.3828608  0.4697008</code></pre>
<p>有了 <code>import_list</code>，是不是连 <code>fitacis</code> 也显得多余了？我们无需用 <code>lapply</code> 导入所有数据再合并数据，并加入一列 factor 来区分我们的数据，省时省力，怎么能叫人不喜欢呢？</p>
</div>
<div id="rioexc" class="section level4">
<h4><span class="header-section-number">3.8.2.2</span> 读取 excel 格式数据</h4>
<p>对于 excel 格式的数据读取，我们只需要正确的输入文件名和扩展名即可，不用管它是 xls 还是 xlsx 的格式。下面我有一个 叫做 aci01.xls 的文件，里面有多个 sheet，我们来读取一个叫做 aci2 的 sheet 内的数据：</p>
<pre class="sourceCode r"><code class="sourceCode r">aci2 &lt;-<span class="st"> </span><span class="kw">import</span>(<span class="st">&quot;./data/aci01.xls&quot;</span>, <span class="dt">sheet =</span> <span class="st">&quot;aci2&quot;</span>)</code></pre>
<p>当然，多数时候我们的数据都是只有一个 sheet 的，或者像我一样不喜欢把所有数据都 分 sheet 放在一个文件的人也很多，所以我们很多时候是不需要这个 <code>sheet = "aci2</code> 这个参数的，但对于某些仪器，一次导出多个 excel 文件也不时很实用，我们需要的数据恰恰又放在了某个 sheet 中，就很实用了，例如 METERS 的仪器喜欢这么做，拿 SATURO 双水头来讲，如果我们需要原始数据来做处理分析：</p>
<pre class="sourceCode r"><code class="sourceCode r">raw_data &lt;-<span class="st"> </span><span class="kw">import</span>(<span class="st">&quot;./data/clay1.xlsx&quot;</span>, <span class="dt">sheet =</span> <span class="st">&quot;Raw Data&quot;</span>)
knitr<span class="op">::</span><span class="kw">kable</span>(<span class="kw">head</span>(raw_data))</code></pre>
<table>
<thead>
<tr class="header">
<th align="right">Record ID</th>
<th align="right">Time (min)</th>
<th align="right">Water Level (cm)</th>
<th align="right">Pressure (cm)</th>
<th align="right">Flux (cm/s)</th>
<th align="right">Volume (mL/s)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="right">4</td>
<td align="right">1</td>
<td align="right">4.35</td>
<td align="right">5.506</td>
<td align="right">0.00122</td>
<td align="right">0.2225459</td>
</tr>
<tr class="even">
<td align="right">5</td>
<td align="right">2</td>
<td align="right">5.07</td>
<td align="right">4.094</td>
<td align="right">0.00000</td>
<td align="right">0.0000000</td>
</tr>
<tr class="odd">
<td align="right">6</td>
<td align="right">3</td>
<td align="right">4.97</td>
<td align="right">4.838</td>
<td align="right">0.00323</td>
<td align="right">0.5891995</td>
</tr>
<tr class="even">
<td align="right">7</td>
<td align="right">4</td>
<td align="right">4.94</td>
<td align="right">5.605</td>
<td align="right">0.00616</td>
<td align="right">1.1236745</td>
</tr>
<tr class="odd">
<td align="right">8</td>
<td align="right">5</td>
<td align="right">4.95</td>
<td align="right">5.264</td>
<td align="right">0.00476</td>
<td align="right">0.8682939</td>
</tr>
<tr class="even">
<td align="right">9</td>
<td align="right">6</td>
<td align="right">4.94</td>
<td align="right">5.424</td>
<td align="right">0.00555</td>
<td align="right">1.0124015</td>
</tr>
</tbody>
</table>
<p>那如果就是喜欢把所有数据放到一个文件里怎么办，答案我们已经见过：</p>
<pre class="sourceCode r"><code class="sourceCode r">all_aci &lt;-<span class="st"> </span><span class="kw">import_list</span>(<span class="st">&quot;./data/aci01.xls&quot;</span>)
<span class="kw">attributes</span>(all_aci)</code></pre>
<pre><code>## $names
## [1] &quot;aci1&quot; &quot;aci2&quot; &quot;aci3&quot;</code></pre>
<p>如果要进一步处理，参考 <a href="readr.html#riocsv">3.8.2.1</a> 内容。</p>
</div>
<div id="riotext" class="section level4">
<h4><span class="header-section-number">3.8.2.3</span> 文本文件的处理</h4>
<p>以上两种格式常见，但有时我们会遇到其他文本格式的数据，即虽然数据为文本格式，但都带有其他特别的后缀，例如 CR1000 的数据，如果你曾经用文本编辑器打开过 CR1000 的数据，很容易看到他是用逗号分隔的，那么我们看一下 <code>rio</code> 的表现：</p>
<pre class="sourceCode r"><code class="sourceCode r">crdata &lt;-<span class="st"> </span><span class="kw">import</span>(<span class="st">&quot;./data/weather.dat&quot;</span>, <span class="dt">format =</span> <span class="st">&quot;,&quot;</span>, <span class="dt">skip =</span> <span class="dv">1</span>)
knitr<span class="op">::</span><span class="kw">kable</span>(<span class="kw">head</span>(crdata))</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">TIMESTAMP</th>
<th align="left">RECORD</th>
<th align="left">Batt_volt_Min</th>
<th align="left">PTemp_C_Max</th>
<th align="left">SR_Wpm2_Avg</th>
<th align="left">PAR_umolpm2s_Avg</th>
<th align="left">NR_uncorrect_Wpm2_Avg</th>
<th align="left">NR_correct_Wpm2_Avg</th>
<th align="left">Soil_hf_Wpm2_Avg</th>
<th align="left">RG_mm_Tot</th>
<th align="left">TCAir_C_Avg</th>
<th align="left">RH_Pcent_Avg</th>
<th align="left">WS_mps_Avg</th>
<th align="left">WS_gust_mps</th>
<th align="left">WD_360_Avg</th>
<th align="left">TCSoil_C_Avg</th>
<th align="left">VWC_Avg</th>
<th align="left">Eb_Avg</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">TS</td>
<td align="left">RN</td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"></td>
<td align="left"></td>
<td align="left">Min</td>
<td align="left">Max</td>
<td align="left">Avg</td>
<td align="left">Avg</td>
<td align="left">Avg</td>
<td align="left">Avg</td>
<td align="left">Avg</td>
<td align="left">Tot</td>
<td align="left">Avg</td>
<td align="left">Avg</td>
<td align="left">Avg</td>
<td align="left">Smp</td>
<td align="left">Avg</td>
<td align="left">Avg</td>
<td align="left">Avg</td>
<td align="left">Avg</td>
</tr>
<tr class="odd">
<td align="left">2018-12-14 16:30:00</td>
<td align="left">0</td>
<td align="left">12.84</td>
<td align="left">6.242</td>
<td align="left">28.16</td>
<td align="left">-56.77</td>
<td align="left">-3.978</td>
<td align="left">-3.978</td>
<td align="left">19.54</td>
<td align="left">0</td>
<td align="left">3.148</td>
<td align="left">40.87</td>
<td align="left">NAN</td>
<td align="left">0</td>
<td align="left">0</td>
<td align="left">0</td>
<td align="left">NAN</td>
<td align="left">NAN</td>
</tr>
<tr class="even">
<td align="left">2018-12-14 17:00:00</td>
<td align="left">1</td>
<td align="left">12.86</td>
<td align="left">4.299</td>
<td align="left">17.75</td>
<td align="left">64.06</td>
<td align="left">-3.7</td>
<td align="left">-3.7</td>
<td align="left">56.13</td>
<td align="left">2</td>
<td align="left">1.623</td>
<td align="left">50.8</td>
<td align="left">0.269</td>
<td align="left">0.13</td>
<td align="left">204.1</td>
<td align="left">0.059</td>
<td align="left">-0.006</td>
<td align="left">1.761</td>
</tr>
<tr class="odd">
<td align="left">2018-12-14 17:30:00</td>
<td align="left">2</td>
<td align="left">12.88</td>
<td align="left">-0.095</td>
<td align="left">0.713</td>
<td align="left">1.433</td>
<td align="left">-3.044</td>
<td align="left">-3.044</td>
<td align="left">26.31</td>
<td align="left">0</td>
<td align="left">-0.695</td>
<td align="left">71.51</td>
<td align="left">0.195</td>
<td align="left">0.3</td>
<td align="left">254.4</td>
<td align="left">-2.945</td>
<td align="left">-0.017</td>
<td align="left">1.28</td>
</tr>
<tr class="even">
<td align="left">2018-12-14 18:00:00</td>
<td align="left">3</td>
<td align="left">12.87</td>
<td align="left">-3.385</td>
<td align="left">-0.006</td>
<td align="left">-0.018</td>
<td align="left">-2.633</td>
<td align="left">-2.633</td>
<td align="left">29.05</td>
<td align="left">0</td>
<td align="left">-1.993</td>
<td align="left">82.6</td>
<td align="left">0.193</td>
<td align="left">0.12</td>
<td align="left">231.9</td>
<td align="left">-3.443</td>
<td align="left">-0.015</td>
<td align="left">1.331</td>
</tr>
<tr class="odd">
<td align="left">不太好的情况是，我们导</td>
<td align="left">入了表头下</td>
<td align="left">面的一行不需要的</td>
<td align="left">内容，这个我们</td>
<td align="left">先忽略，后面再</td>
<td align="left">处理。</td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
<td align="left"></td>
</tr>
</tbody>
</table>
<p>对于 LI-840 等 txt 数据，喜欢用空格分列，使用 <code>rio</code> 的效果非常好，直接自动删除了第一个头文件的日期，读取了我测量的数据：</p>
<pre class="sourceCode r"><code class="sourceCode r">li840 &lt;-<span class="st"> </span><span class="kw">import</span>(<span class="st">&quot;./data/li840.txt&quot;</span>)
knitr<span class="op">::</span><span class="kw">kable</span>(<span class="kw">head</span>(li840))</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">Date(Y-M-D)</th>
<th align="left">Time(H:M:S)</th>
<th align="right">CO2(ppm)</th>
<th align="right">H2O(ppt)</th>
<th align="right">H2O(C)</th>
<th align="right">Cell_Temperature(C)</th>
<th align="right">Cell_Pressure(kPa)</th>
<th align="right">CO2_Absorption</th>
<th align="right">H2O_Absorption</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">2017-06-14</td>
<td align="left">11:07:19</td>
<td align="right">707.61</td>
<td align="right">18.76</td>
<td align="right">16.49</td>
<td align="right">52.19</td>
<td align="right">100.37</td>
<td align="right">0.1328</td>
<td align="right">0.1067</td>
</tr>
<tr class="even">
<td align="left">2017-06-14</td>
<td align="left">11:07:20</td>
<td align="right">707.62</td>
<td align="right">18.76</td>
<td align="right">16.49</td>
<td align="right">52.19</td>
<td align="right">100.37</td>
<td align="right">0.1328</td>
<td align="right">0.1067</td>
</tr>
<tr class="odd">
<td align="left">2017-06-14</td>
<td align="left">11:07:21</td>
<td align="right">707.78</td>
<td align="right">18.77</td>
<td align="right">16.49</td>
<td align="right">52.19</td>
<td align="right">100.35</td>
<td align="right">0.1328</td>
<td align="right">0.1067</td>
</tr>
<tr class="even">
<td align="left">2017-06-14</td>
<td align="left">11:07:22</td>
<td align="right">707.87</td>
<td align="right">18.77</td>
<td align="right">16.50</td>
<td align="right">52.19</td>
<td align="right">100.35</td>
<td align="right">0.1328</td>
<td align="right">0.1067</td>
</tr>
<tr class="odd">
<td align="left">2017-06-14</td>
<td align="left">11:07:23</td>
<td align="right">707.78</td>
<td align="right">18.77</td>
<td align="right">16.50</td>
<td align="right">52.19</td>
<td align="right">100.35</td>
<td align="right">0.1328</td>
<td align="right">0.1067</td>
</tr>
<tr class="even">
<td align="left">2017-06-14</td>
<td align="left">11:07:24</td>
<td align="right">707.84</td>
<td align="right">18.77</td>
<td align="right">16.50</td>
<td align="right">52.19</td>
<td align="right">100.35</td>
<td align="right">0.1328</td>
<td align="right">0.1067</td>
</tr>
</tbody>
</table>


</div>
</div>
</div>
</div>
<div class="footnotes">
<hr />
<ol start="2">
<li id="fn2"><p>R 内没有 float<a href="readr.html#fnref2" class="footnote-back">↩</a></p></li>
<li id="fn3"><p>有时候我会把省掉的补充上，如果后面遇到不得不详细讲的内容时。<a href="readr.html#fnref3" class="footnote-back">↩</a></p></li>
<li id="fn4"><p>农学的童鞋都理解吧，我记得我当时生物统计教材将处理和水平时就用的不同施肥，虽然当时没学好，这么“土的掉渣”的例子我还记得。<a href="readr.html#fnref4" class="footnote-back">↩</a></p></li>
<li id="fn5"><p>一年当中的第几天<a href="readr.html#fnref5" class="footnote-back">↩</a></p></li>
<li id="fn6"><p>unix时间，从1970年1月1日开始计算，距离它的秒来几时<a href="readr.html#fnref6" class="footnote-back">↩</a></p></li>
</ol>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="prerequisites.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="rioexport.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": true,
"facebook": false,
"twitter": true,
"google": false,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "google", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/zhujiedong/tidyphotoanalysis/blob/master/02-1-1-import.Rmd",
"text": "缂栬緫"
},
"history": {
"link": null,
"text": null
},
"download": ["bookdown.pdf"],
"toc": {
"collapse": "none"
}
});
});
</script>

</body>

</html>
